package com.timmy.test1.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.timmy.test1.R;

/**
 * Created by timmy on 2020/5/6.
 */
public class ImageSeekBar extends View {
    private boolean isDrawBubble;

    private boolean isTouchSlider;

    private int mBottom;

    private Bitmap mBubbleBitmap;

    private int mBubbleHeight;

    private int mBubbleWidth;

    private float mCentenX;

    private Paint mCirclePaint;

    private int mClipRight;

    private final Context mContext;

    private int mCurrentProgress;

    private boolean mIsShowBubble;

    private onDrawTextListener mListener;

    private int mMargin = 20;

    private Rect mMeasuredRect;

    private int mOffseLeft;

    private float[] mProgressArr;

    private OnProgressChangedListener mProgressListener;

    private RectF mRectBubble;

    private int mSliderHeight;

    private int mSliderWidth;

    private int mSplitCount = 3;

    private Bitmap mTickMarkBitmap;

    private int mTop;

    private Bitmap mTrackBitmap;

    private int mTrackHeight;

    private int mTrackOffset;

    private int mTrackWidth;

    private int mViewHeight;

    private int mbubbleOffset;

    String[] text = new String[]{"低", "中", "高"};

    public ImageSeekBar(Context paramContext) {
        this(paramContext, (AttributeSet) null);
        init(paramContext);
    }

    public ImageSeekBar(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
        this.mContext = paramContext;
        init(paramContext);
        initAttrs(this.mContext, paramAttributeSet);
    }

    public ImageSeekBar(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
        super(paramContext, paramAttributeSet, paramInt);
        this.mContext = paramContext;
        init(paramContext);
        initAttrs(this.mContext, paramAttributeSet);
    }

    private void drawBubble(Canvas paramCanvas) {
        if (this.mBubbleBitmap != null && this.isDrawBubble) {
            float f;
            int i = this.mClipRight;
            int j = this.mBubbleWidth;
            int m = j / 2;
            int k = this.mOffseLeft;
            if (i < m + k) {
                f = (j / 2 + k);
            } else {
                m = this.mTrackWidth;
                if (i > m - j / 2 + k) {
                    f = (m + k - j / 2);
                } else {
                    f = i;
                }
            }
            RectF rectF = new RectF(f - (this.mBubbleWidth / 2), 0.0F, (this.mBubbleWidth / 2) + f, this.mBubbleHeight);
            this.mRectBubble = rectF;
            paramCanvas.drawBitmap(this.mBubbleBitmap, null, rectF, this.mCirclePaint);
            drawText(getCurrentProgress(), paramCanvas);
        }
    }

    private void drawSlider(Canvas paramCanvas) {
        int i = this.mClipRight;
        int j = this.mSliderWidth;
        int m = j / 2;
        int k = this.mOffseLeft;
        if (i < m + k) {
            this.mCentenX = (j / 2 + k);
        } else {
            m = this.mTrackWidth;
            if (i > m + k - j / 2) {
                this.mCentenX = (m + k - j / 2);
            } else {
                this.mCentenX = i;
            }
        }
        paramCanvas.drawCircle(this.mCentenX, this.mMeasuredRect.centerY(), (this.mSliderHeight / 2), this.mCirclePaint);
    }

    private void drawText(int paramInt, Canvas paramCanvas) {
        String str;
        Paint paint = new Paint();
        paint.setColor(-1);
        paint.setTextSize(30.0F);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        paint.setTextAlign(Paint.Align.CENTER);
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float f1 = fontMetrics.top;
        float f2 = fontMetrics.bottom;
        int i = (int) (this.mRectBubble.centerY() - f1 / 2.0F - f2 / 2.0F);
        onDrawTextListener onDrawTextListener1 = this.mListener;
        if (onDrawTextListener1 != null) {
            str = onDrawTextListener1.getOnDrawText(paramInt);
        } else {
            str = String.valueOf(paramInt);
        }
        paramCanvas.drawText(str, this.mRectBubble.centerX(), i, paint);
    }

    private void drawTickMarks(Canvas paramCanvas) {
        if (this.mTickMarkBitmap != null) {
            paramCanvas.save();
            paramCanvas.clipRect(0, this.mTop, this.mClipRight, this.mBottom);
            paramCanvas.drawBitmap(this.mTickMarkBitmap, null, this.mMeasuredRect, this.mCirclePaint);
            paramCanvas.restore();
        }
    }

    private void drawTrack(Canvas paramCanvas) {
        Bitmap bitmap = this.mTrackBitmap;
        if (bitmap != null)
            paramCanvas.drawBitmap(bitmap, null, this.mMeasuredRect, this.mCirclePaint);
    }

    private int getClosestIndex() {
        float f = this.mTrackWidth;
        int i = 0;
        int j = 0;
        while (true) {
            float[] arrayOfFloat = this.mProgressArr;
            if (i < arrayOfFloat.length) {
                float f2 = Math.abs(arrayOfFloat[i] - (this.mClipRight - this.mOffseLeft));
                float f1 = f;
                if (f2 <= f) {
                    j = i;
                    f1 = f2;
                }
                i++;
                f = f1;
                continue;
            }
            return j;
        }
    }

    private void init(Context paramContext) {
        Paint paint = new Paint();
        this.mCirclePaint = paint;
        paint.setColor(-1);
        this.mCirclePaint.setAntiAlias(true);
        this.mCirclePaint.setStyle(Paint.Style.FILL);
    }

    private void initAttrs(Context paramContext, AttributeSet paramAttributeSet) {
        TypedArray typedArray = paramContext.obtainStyledAttributes(paramAttributeSet, R.styleable.ImageSeekBarView);
        this.mIsShowBubble = typedArray.getBoolean(R.styleable.ImageSeekBarView_is_show_bubble, false);
        this.mCurrentProgress = (int) typedArray.getFloat(R.styleable.ImageSeekBarView_progress, 0.0F);
        this.mTrackBitmap = BitmapFactory.decodeResource(getResources(), typedArray.getResourceId(R.styleable.ImageSeekBarView_track_drawable, 0));
        this.mTickMarkBitmap = BitmapFactory.decodeResource(getResources(), typedArray.getResourceId(R.styleable.ImageSeekBarView_tick_mark_drawable, 0));


        this.mTrackHeight = AutoSizeUtil.getAutoWidth(getContext(), typedArray.getDimensionPixelSize(R.styleable.ImageSeekBarView_track_height, 50));
        this.mTrackWidth = AutoSizeUtil.getAutoWidth(getContext(), typedArray.getDimensionPixelSize(R.styleable.ImageSeekBarView_track_width, 800));
        this.mSliderHeight = AutoSizeUtil.getAutoWidth(getContext(), typedArray.getDimensionPixelSize(R.styleable.ImageSeekBarView_slider_height, 50));
        this.mSliderWidth = AutoSizeUtil.getAutoWidth(getContext(), typedArray.getDimensionPixelSize(R.styleable.ImageSeekBarView_slider_width, 200));
        if (this.mIsShowBubble) {
            this.mBubbleHeight = AutoSizeUtil.getAutoWidth(getContext(), typedArray.getDimensionPixelSize(R.styleable.ImageSeekBarView_bubble_height, 50));
            this.mBubbleWidth = AutoSizeUtil.getAutoWidth(getContext(), typedArray.getDimensionPixelSize(R.styleable.ImageSeekBarView_bubble_height, 200));
            this.mBubbleBitmap = BitmapFactory.decodeResource(getResources(), typedArray.getResourceId(R.styleable.ImageSeekBarView_bubble_drawable, 0));
            return;
        }
        this.mMargin = 0;
    }

    public int getCurrentProgress() {
        int i = (this.mClipRight - this.mOffseLeft) * 100 / this.mTrackWidth;
        this.mCurrentProgress = i;
        return i;
    }

    public float getCurrentProgressF() {
        return (this.mClipRight - this.mOffseLeft) * 100.0F / this.mTrackWidth;
    }

    @Override
    public void onDraw(Canvas paramCanvas) {
        drawTrack(paramCanvas);
        drawTickMarks(paramCanvas);
        drawSlider(paramCanvas);
        if (this.mIsShowBubble) {
            drawBubble(paramCanvas);
        }
    }

    @Override
    protected void onMeasure(int paramInt1, int paramInt2) {
        super.onMeasure(paramInt1, paramInt2);
        paramInt1 = this.mBubbleHeight + this.mSliderHeight + this.mMargin;
        this.mViewHeight = paramInt1;
        setMeasuredDimension(this.mTrackWidth, paramInt1);
        if (this.mIsShowBubble) {
            paramInt1 = this.mBubbleHeight;
            if (paramInt1 > 0) {
                this.mbubbleOffset = paramInt1;
            }
        }
        paramInt1 = this.mSliderHeight;
        paramInt2 = this.mTrackHeight;
        if (paramInt1 > paramInt2) {
            this.mTrackOffset = this.mBubbleHeight + (paramInt1 - paramInt2) / 2;
        }
        this.mOffseLeft = (getMeasuredWidth() - this.mTrackWidth) / 2 + getPaddingLeft();
        paramInt1 = 0;
        this.mOffseLeft = 0;
        paramInt2 = this.mMargin + this.mTrackOffset;
        this.mTop = paramInt2;
        this.mBottom = paramInt2 + this.mTrackHeight;
        paramInt2 = this.mOffseLeft;
        this.mMeasuredRect = new Rect(paramInt2, this.mTop, this.mTrackWidth + paramInt2, this.mBottom);
        this.mClipRight = this.mCurrentProgress * this.mTrackWidth / 100;
        this.mProgressArr = new float[this.mSplitCount];
        while (true) {
            float[] arrayOfFloat = this.mProgressArr;
            if (paramInt1 < arrayOfFloat.length) {
                int i = this.mTrackWidth;
                paramInt2 = this.mSplitCount;
                if (paramInt2 - 1 > 0) {
                    paramInt2--;
                } else {
                    paramInt2 = 1;
                }
                arrayOfFloat[paramInt1] = (i * paramInt1 / paramInt2);
                paramInt1++;
                continue;
            }
            break;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent paramMotionEvent) {
        int i = (int) paramMotionEvent.getX();
        int j = (int) paramMotionEvent.getY();
        int k = this.mTrackWidth;
        int m = this.mSplitCount;
        boolean bool = true;
        k /= m - 1;
        k = paramMotionEvent.getActionMasked();
        if (k != 0) {
            if (k != 1) {
                if (k != 2) {
                    if (k != 3) {
                        if (k != 5) {
                            if (k != 6) {
                                i = 0;
                            } else {
                                invalidate();
                                i = 1;
                            }
                        } else {
                            invalidate();
                            i = 1;
                        }
                        if (!super.onTouchEvent(paramMotionEvent)) {
                            if (i != 0)
                                return true;
                            bool = false;
                        }
                        return bool;
                    }
                } else {
                    i = (int) paramMotionEvent.getX();
                    if (this.isDrawBubble) {
                        this.mClipRight = i;
                        j = this.mTrackWidth;
                        k = this.mOffseLeft;
                        if (i > j + k) {
                            this.mClipRight = j + k;
                        } else if (i < k) {
                            this.mClipRight = k;
                        }
                        OnProgressChangedListener onProgressChangedListener = this.mProgressListener;
                        if (onProgressChangedListener != null)
                            onProgressChangedListener.onProgressChanged(getCurrentProgress(), getCurrentProgressF());
                    }
                    invalidate();
                }
            } else {
                this.isDrawBubble = false;
                this.isTouchSlider = false;
                invalidate();
            }
        } else {
            float f1 = this.mCentenX;
            k = this.mSliderWidth;
            float f2 = k;
            float f3 = i;
            if (f1 - f2 < f3 && f3 < f1 + k && this.mMeasuredRect.centerY() - this.mSliderWidth < j && j < this.mMeasuredRect.centerY() + this.mSliderWidth)
                this.isTouchSlider = true;
            if (this.mMeasuredRect.centerY() - this.mSliderWidth < j && j < this.mMeasuredRect.centerY() + this.mSliderWidth) {
                this.mClipRight = i;
                this.isDrawBubble = true;
            }
            invalidate();
        }
        i = 1;

        return false;
    }

    public void setOnDrawTextListener(onDrawTextListener paramonDrawTextListener) {
        this.mListener = paramonDrawTextListener;
    }

    public void setOnProgressChangeListener(OnProgressChangedListener paramOnProgressChangedListener) {
        this.mProgressListener = paramOnProgressChangedListener;
    }

    public void setProgress(int paramInt) {
        this.mCurrentProgress = paramInt;
        requestLayout();
    }

    public static interface OnProgressChangedListener {
        void onProgressChanged(int param1Int, float param1Float);
    }

    public static interface onDrawTextListener {
        String getOnDrawText(int param1Int);
    }
}